package cn.gd.controller.myDesktop.frequentlyUsedURLs;

import cn.gd.pojo.FrequentlyUsedURLs;
import cn.gd.service.frequentlyUsedURLs.FrequentlyUsedURLsService;
import cn.gd.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/frequentlyUsedURLs")
public class FrequentlyUsedURLsController {
    @Autowired
    private FrequentlyUsedURLsService service;

    @GetMapping("/queryFrequentlyUsedURLs/{pageNum}/{pageSize}/{userID}")
    public Result<Object> queryFrequentlyUsedURLs(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @PathVariable Integer userID
    ){
        PageInfo<FrequentlyUsedURLs> frequentlyUsedURLsPageInfo = service.queryFrequentlyUsedURLs(pageNum, pageSize, userID);
        if (frequentlyUsedURLsPageInfo != null) {
            return Result.ok(frequentlyUsedURLsPageInfo);
        }
        return Result.error().message("无任何数据");
    }

    @PutMapping("/updateFrequentlyUsedURLs")
    public Result updateFrequentlyUsedURLs(@RequestBody FrequentlyUsedURLs frequentlyUsedURLs){
        if (service.updateFrequentlyUsedURLsById(frequentlyUsedURLs)) {
            return Result.ok().message("修改成功");
        }

        return Result.error().message("修改失败!!!");
    }

    @DeleteMapping("/delFrequentlyUsedURLsById/{id}")
    public Result delFrequentlyUsedURLsById(@PathVariable Integer id){
        if (service.deleteFrequentlyUsedURLsById(id)) {
            return Result.ok().message("删除成功");
        }
        return Result.error().message("删除失败!!!");
    }

    @PostMapping("/addFrequentlyUsedURLs")
    public Result addFrequentlyUsedURLs(@RequestBody FrequentlyUsedURLs frequentlyUsedURLs){
        if (service.addFrequentlyUsedURLs(frequentlyUsedURLs)) {
            return Result.ok().message("新增成功");
        }
        return Result.error().message("新增失败!!!");
    }
}
